package cn.itcast.service.impl;

import cn.itcast.dao.MemberDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.enums.Constants;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberService;
import cn.itcast.utils.JwtUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private MemberDao memberDao;
    /**
     * 校验登录
     * @param map
     * @return
     */
    public Result memberLogin(Map map) {
        //1.在map集合中获取到手机号和验证码
        String phone = (String) map.get("phone"); //手机号
        String validateCode = (String) map.get("validateCode"); //验证码
        //2.判断手机号或者验证码是否为空
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(validateCode)){
            return new Result(false,"手机号或用户名为空",null);
        }
        //3.拼接key
        String key = Constants.SMS_CODE+phone;
        //4.根据key获取value,并判断value是否存在
        String value = redisTemplate.opsForValue().get(key);
        if (StringUtils.isEmpty(value)){
            return new Result(false,"验证码不存在",null);
        }
        //5.如果存在,判断验证码是否相等
        if (!validateCode.equals(value)){
            return new Result(false,"验证码错误",null);
        }

        //6.相等,从redis中删除验证码
        redisTemplate.delete(key);
        //7.调用mapper层判断是否是新会员
        Member member=memberDao.findByPhone(phone);
        //构建member,新会员,存进表中
        if (member==null){
            member=new Member();
            member.setPhoneNumber(phone);
            member.setRegTime(new Date());
            //调用dao层保存到表中
            memberDao.save(member);
        }


        //生成JWT-Token
        Map<String,Object> memberMap=new HashMap<>();
        memberMap.put("id",member.getId());
        memberMap.put("phone",phone);
        String token = JwtUtils.getToken(memberMap);

        //返回
        return new Result(true,"登录成功",token);
    }


    /**
     * 根据电话查询会员详情
     * @param phone
     * @return
     */
    @Override
    public Member findByPhone(String phone) {
        return memberDao.findByPhone(phone);
    }


    /**
     * 分页查询会员资料
     *
     * @param currentPage
     * @param pageSize
     * @param queryString 查询条件 （档案号/姓名号/手机号）
     * @return
     */
    @Override
    public PageResult findByPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        List<Member> list = null;
        if (org.apache.commons.lang3.StringUtils.isEmpty(queryString)) {
            list = memberDao.findPage();
        }else {
            list = memberDao.findPageByCondition(queryString);
        }
        PageInfo<Member> pageInfo = new PageInfo<>(list);
        return new PageResult(pageInfo.getTotal(), list);
    }

    /**
     * 根据id查找会员，进行页面回显
     *
     * @param id
     * @return
     */
    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    /**
     * 新增会员
     * @param member
     */
    @Override
    public void add(Member member) {
        member.setRegTime(new Date());
        memberDao.insert(member);
    }

    @Override
    public void update(Member member) {
        memberDao.update(member);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        memberDao.updateStatus(id,status);
    }



}
